/*
 * $Id$
 *
 * Copyright 2008 HuangHuang. All rights reserved.
 */
package com.crm.web.page;

import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.annotations.Bean;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationDelegate;
import org.apache.tapestry.IPage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.crm.model.User;
import com.crm.model.LProduct;
import com.crm.model.Listbox;
import com.crm.service.IListboxService;
import com.javaforge.tapestry.spring.annotations.InjectSpring;

import java.util.List;
import java.util.ArrayList;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public abstract class SysListbox extends BasePage  implements PageBeginRenderListener {

    private static Log logger = LogFactory.getLog(SysListbox.class);
    private static String listboxName;
    
    private boolean addItem;

    @InjectSpring("listboxService")
    public abstract IListboxService getListboxServ();

    @InjectState("userSession")
    public abstract User getUserSession();

    @Bean
    public abstract ValidationDelegate getDelegate();

    public abstract String getListItemName();

    public void pageBeginRender(PageEvent event) {
        if (getUserSession().getUsername() == null || !getUserSession().getUserGroup().equals(User.GROUP_SUPER)) {
            event.getRequestCycle().sendRedirect("app?page=SysPageError&service=page");
        } else if (listboxName == null) {
            listboxName = LProduct.class.getName();
        }
    }

    public List getEntries() {
        try {
            return getListboxServ().getList(listboxName);
        }
        catch (Exception e) {
            String exceptionString = "系统异常：" + e.getMessage();
            logger.error(exceptionString);
            e.printStackTrace();
            getDelegate().record(exceptionString, ValidationConstraint.CONSISTENCY);
            return new ArrayList();
        }
    }

    public String getListboxName() {
        return listboxName;
    }

    public void setListboxName(String listboxName0) {
        listboxName = listboxName0;
    }

    public IPage switchListbox() {
        return this;
    }

    public IPage saveItem() {
        try {
            getListboxServ().create(listboxName, getListItemName());
            addItem = false;
        } catch (Exception e) {
            String s = "系统异常：" + e;
            logger.error(s);
            e.printStackTrace();
            getDelegate().record(s, ValidationConstraint.CONSISTENCY);
        }
        return this;
    }

    public boolean isAddItem() {
        return addItem;
    }

    public IPage newItem() {
        addItem = true;
        return this;
    }

    public void deleteListBox(Listbox list) {
        try {
            getListboxServ().delete(list);
        } catch (Exception e) {
            String s = "要删除的列表项可能已经被引用，请先取消其他记录中对此项的引用，然后再删除！";
            logger.error(s + e);
            e.printStackTrace();
            getDelegate().record(s, ValidationConstraint.CONSISTENCY);
        }
    }

}
